<?php
class TaskAction extends CommonAction{
	public function data(){
		$method = $_POST;
		$methodget = $_GET;
		$uObj = new UserModel();
		$yObj = D('yuyue');
		$zixunObj = D('zixun');
		switch($method['do']){
			case 'leftid':
				$array = array();
				$array[] = array('id' => 1 ,'text' => '次数','leaf' => true);
				$array[] = array('id' => 2 ,'text' => '月任务','leaf' => true);
				//$array[] = array('id' => 'all' ,'text'=> '全组统计','leaf' => true);
				echo json_encode($array);
			break;
			case 'id':
				$map['uid'] = $method['where'];
				$map['date'] = strtotime($method['date']);
				$zixun = $zixunObj->where($map)->find();
				echo json_encode($zixun);
			break;
			default:
			switch($method['do']){
				default:
					$d = getcity();
					$userinfo = Session::get('userinfo');
					switch($userinfo['type_id']){
						case 1:
							foreach($uObj->field('id,account,nickname')->page(intval($methodget['page']),intval($methodget['limit']))->select() as $v){
								$tongji[] = $v;
								$ids[] = $v['id'];
							}
							//$map['date'] = strtotime($method['dates']);
							$count = $uObj->count();
						break;
						case 2:
							$usermap['did'] = $userinfo['did'];
							foreach($uObj->field('id,account,nickname')->where($usermap)->page(intval($methodget['page']),intval($methodget['limit']))->select() as $v){
								$tongji[] = $v;
								$ids[] = $v['id'];
							}
							$count = $uObj->where($usermap)->count();
						break;
					}
					$map['uid'] = array('in',$ids);
					$map['date'] = strtotime($method['date']);
					foreach($zixunObj->where($map)->select() as $v){
						$zixuns[$v['uid']] = $v;
					}
					foreach($tongji as $v){
						$v['yuyue'] = $zixuns[$v['id']]['yuyue'];
						$v['jiuzhen'] = $zixuns[$v['id']]['jiuzhen'];
						$v['zixun'] = $zixuns[$v['id']]['zixun'];
						$v['youxiao'] = $zixuns[$v['id']]['youxiao'];
						$v['zongyouxiao'] = $zixuns[$v['id']]['zongyouxiao'];
						//$v['date'] = date('r',$jobs[$v['id']]['date']);
						$tongjis[] = $v;
					}
					echo json_encode(array('list'=>$tongjis,'count'=>$count));
				break;
			}
			break;
		}
	}
	public function jobdata(){
		$method = $_POST;
		$methodget = $_GET;
		$uObj = new UserModel();
		$jobObj = D('job');
		switch($method['do']){
			case 'id':
				$map['uid'] = $method['where'];
				$map['date'] = strtotime($method['date']);
				$job = $jobObj->where($map)->find();
				echo json_encode($job);
			break;
			default:
			switch($method['do']){
				default:
					$d = getcity();
					$userinfo = Session::get('userinfo');
					switch($userinfo['type_id']){
						case 1:
							foreach($uObj->field('id,account,nickname')->page(intval($methodget['page']),intval($methodget['limit']))->select() as $v){
								$tongji[] = $v;
								$ids[] = $v['id'];
							}
							//$map['date'] = strtotime($method['dates']);
							$count = $uObj->count();
						break;
						case 2:
							$usermap['did'] = $userinfo['did'];
							foreach($uObj->field('id,account,nickname')->where($usermap)->page(intval($methodget['page']),intval($methodget['limit']))->select() as $v){
								$tongji[] = $v;
								$ids[] = $v['id'];
							}
							$count = $uObj->where($usermap)->count();
						break;
					}
					$map['uid'] = array('in',$ids);
					$map['date'] = strtotime($method['date']);
					foreach($jobObj->where($map)->select() as $v){
						$jobs[$v['uid']] = $v;
					}

					foreach($tongji as $v){
						//$v['date'] = date('r',$jobs[$v['id']]['date']);
						$v['jiuzhen'] = $jobs[$v['id']]['jiuzhen'];
						$tongjis[] = $v;
					}
					echo json_encode(array('list'=>$tongjis,'count'=>$count));
				break;
			}
			break;
		}
	}
	public function index(){
		$this->display();
	}
	public function edit() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$userObj = new UserModel();
			$zixunObj = D('zixun');
			$mapdate['date'] = strtotime($method['dates']);
			$mapdate['uid'] = $method['uid'];
			if($zixun = $zixunObj->where($mapdate)->find())
			{
				$zixunObj->id = $zixun['id'];
				$zixunObj->yuyue = $method['yuyue'];
				$zixunObj->jiuzhen = $method['jiuzhen'];
				$zixunObj->zixun = $method['zixun'];
				$zixunObj->youxiao = $method['youxiao'];
				$zixunObj->zongyouxiao = $method['zongyouxiao'];
				if($zixunObj->save()){
					$this->success('修改成功');
				}else{
					//print_r($zixunObj);
					$this->error('修改失败');
				}
			}else{

				$data['uid'] = $method['uid'];
				$data['yuyue'] = $method['yuyue'];
				$data['jiuzhen'] = $method['jiuzhen'];
				$data['zixun'] = $method['zixun'];
				$data['youxiao'] = $method['youxiao'];
				$data['zongyouxiao'] = $method['zongyouxiao'];
				$data['date'] = strtotime($method['dates']);
				if($zixunObj->add($data)){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}

			}

		}
	}
	public function jobedit(){
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$userObj = new UserModel();
			$jobObj = D('job');
			$mapdate['date'] = strtotime($method['dates']);
			$mapdate['uid'] = $method['uid'];
			if($job = $jobObj->where($mapdate)->find())
			{

				$jobObj->id = $job['id'];
				$jobObj->jiuzhen = $method['jiuzhen'];
				if($jobObj->save()){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}else{
				$data['uid'] = $method['uid'];
				$data['jiuzhen'] = $method['jiuzhen'];
				$data['date'] = strtotime($method['dates']);
				if($jobObj->add($data)){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}

			}

		}
	}
}